/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func convertBST(root *TreeNode) *TreeNode {
	sum := 0
	dfs(root, &sum)
	return root
}

func dfs(node *TreeNode, sum *int) {
	if node != nil {
		dfs(node.Right, sum)
		*sum += node.Val
		node.Val = *sum
		dfs(node.Left, sum)
	}
}